/***************
 * AIR2 Upload Form Component
 *
 * FormPanel used to upload a csv file to AIR2
 */
AIR2.Upload.FileForm = function() {
    var upfield = new Ext.ux.form.FileUploadField({
        name: 'csvfile',
        width: 250,
        allowBlank: false,
        hideLabel: true,
        msgTarget: 'under',
        buttonCfg: {
            xtype: 'air2button',
            air2type: 'BLUE',
            air2size: 'MEDIUM'
        }
    });
    var progress = new Ext.ProgressBar({
        width: 250,
        hidden: true
    });
    var statusfield = new Ext.form.DisplayField({
        cls: 'upload-form-msg',
        hideLabel: true,
        msgTarget: 'under'
    });

    var panel = new Ext.form.FormPanel({
        cls: 'air2-upload-form',
        unstyled: true,
        fileUpload: true,
        padding: '2px 20px',
        items: [{
            /* allows ajax-ish file-upload */
            xtype: 'hidden',
            name: 'air2_mismatch_content',
            value: 'text/html'
        },{
            xtype: 'fieldset',
            title: 'File Upload',
            items: [{
                xtype: 'box',
                style: 'padding-bottom:10px',
                html: 'For formatting help, use the <a href="' + AIR2.HOMEURL +
                    '/files/csv/upload-template.xlt">CSV Template</a> file.'
            },{
                xtype: 'container',
                layout: 'column',
                items: [{
                    xtype: 'container',
                    columnWidth: .45,
                    layout: 'form',
                    items: [upfield, progress]
                },{
                    xtype: 'container',
                    columnWidth: .55,
                    layout: 'form',
                    items: statusfield
                }]
            }]
        }],
        setTankData: function(data) {
            this.tankUUID = data.tank_uuid;
            if (data.tank_file_name) {
                var meta = Ext.decode(data.tank_meta);
                upfield.setValue(data.tank_file_name);
                statusfield.setValue('File Size: '+meta.file_size+'<br/>Uploaded on: ' +
                    AIR2.Format.dateLong(data.tank_upd_dtim));
            }

            if (data.tank_status == 'N' || data.tank_status == 'F') {
                upfield.enable();
            }
            else {
                upfield.disable();
            }
        }
    });

    upfield.on('fileselected', function(fld, name) {
        if (name) {
            var f = panel.getForm();
            progress.reset().show().wait({text: 'Uploading File...'});
            statusfield.clearInvalid();

            // determine the url
            var url = AIR2.HOMEURL+'/upload.json';
            if (panel.tankUUID) {
                url = AIR2.HOMEURL+'/upload/'+panel.tankUUID+'.json';
            }

            // submit the form
            f.submit({
                url: url,
                success: function(f, o) {
                    progress.reset().hide();
                    panel.fireEvent('afterupload', true, o.result);
                    upfield.lastSelected = upfield.getValue();

                    // unset field value, so onChange will fire next time
                    upfield.fileInput.dom.value = '';
                },
                failure: function(f, o) {
                    progress.reset().hide();
                    panel.fireEvent('afterupload', false, o.result);
                    statusfield.markInvalid(o.result.message);

                    // unset field value, so onChange will fire next time
                    upfield.fileInput.dom.value = '';
                }
            });
        }
        else {
            // user cancelled --- set to last value
            upfield.setValue(upfield.lastSelected);
        }
    });

    return panel;
}